package com.game.worm.mobile.view.details;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.common.string.StringUtil;
import com.dx.as.http.IHttpListener;
import com.game.worm.mobile.R;
import com.game.worm.mobile.bean.GameBean;
import com.game.worm.mobile.view.GameExtras;
import com.game.worm.mobile.view.http.HttpHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by d_xxiang on 2018/5/18.
 */

public class DetailsActivity extends UI{
    private TabLayout tabLayout;
    private GameBean gameInfo;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_details);
        initView();
        getDetails();
    }

    private void getDetails(){
        String appId = getIntent().getStringExtra(GameExtras.EXTRAS_GAME_ID);
        HttpHelper.getGameDetials(appId,httpListener);
    }

    private void setToolBar(){
        ToolBarOptions toolBarOptions = new ToolBarOptions();
        toolBarOptions.titleString = gameInfo.getName();
        setToolbar(toolBarOptions);
    }

    private void setViewData(){
        ImageView ico = findView(R.id.ico);
        TextView name = findView(R.id.name);
        TextView score = findView(R.id.score);
        TextView updateDsc = findView(R.id.updateDsc);
        TextView remmber = findView(R.id.remmber);
        TextView intro = findView(R.id.intro);
        TextView introduce = findView(R.id.introduce);
        TextView size = findView(R.id.size);
        TextView version = findView(R.id.version);
        TextView update_time = findView(R.id.update_time);
        LinearLayout tag_container = findView(R.id.tag_container);
        RecyclerView recyclerImg = findView(R.id.recyclerImg);

        ImageLoader.getInstance().displayImage(gameInfo.getImgUrlIcon(),ico);
        name.setText(gameInfo.getName());
        score.setText(gameInfo.getGameScore());
        updateDsc.setText(gameInfo.getUpdateDes());
        remmber.setText(gameInfo.getReminder());
        intro.setText(gameInfo.getIntro());
        introduce.setText(gameInfo.getIntroduce());
        size.append(gameInfo.getGameSize());
        version.append(gameInfo.getVersion());
        update_time.append(gameInfo.getUpdateTime() == null ?"":gameInfo.getUpdateTime());

        if (!StringUtil.isEmpty(gameInfo.getImgUrlBig())){
            String [] imgs = gameInfo.getImgUrlBig().split("|");
        }

    }



    private void initView(){
        tabLayout = findViewById(R.id.tabs);
        String[] titles = getResources().getStringArray(R.array.details_tab);
        for (int i = 0; i < titles.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText(titles[i]));
        }
        tabLayout.addOnTabSelectedListener(onTabSelectedListener);
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

    private IHttpListener httpListener = new IHttpListener() {
        @Override
        public void onFilure(int code, String errorMsg) {
            super.onFilure(code, errorMsg);
        }

        @Override
        public void onResponse(Object response, int flg) {
           if (response != null){
               gameInfo = (GameBean) response;
               setToolBar();
               setViewData();
           }
        }

        @Override
        public void onFailure(Throwable t) {
            super.onFailure(t);
        }
    };
}
